using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.PlayerLoop;

public class SoundMgr : SingletonMono<SoundMgr>
{
    private SoundData bgSoundData = new SoundData();
    private SoundData talkSoundData = new SoundData();
    private SoundData soundData = new SoundData();

    public override void Awake()
    {
        base.Awake();
        Init();
    }

    // 初始化
    void Init()
    {
        bgSoundData.OnInit("BkMusic", true, "music_muted", "music_volume");
        talkSoundData.OnInit("TalkSound", true, "talk_muted", "talk_volume");
        soundData.OnInit("Sound", false, "sound_muted", "sound_volume");
    }

    private void Update()
    {
        soundData.Update();
        talkSoundData.Update();
    }

    SoundData GetSoundData(int id = 0)
    {
        switch (id)
        {
            case 0: return soundData;
            case 1: return bgSoundData;
            case 2: return talkSoundData;

            default:
                break;
        }
        return null;
    }


    #region 播放音效
    // 播放音效和语音？
    public void PlaySound(string name, bool loop = false, UnityAction<AudioSource> callBack = null, int id = 0)
    {
        GetSoundData(id)?.PlaySound(name, loop, callBack);
    }

    // 停止音效
    public void StopSound(AudioSource source, int id = 0)
    {
        GetSoundData(id)?.StopSound(source);
    }

    // 停驶所有音效
    public void StopSound(int id = 0)
    {
        GetSoundData(id)?.StopSound();
    }

    // 暂停对话语音
    public void PauseTalkSound(AudioSource source, int id = 0)
    {
        GetSoundData(id)?.PauseTalkSound(source);
    }

    // 停止对话语音
    public void PauseTalkSound(int id = 0)
    {
        GetSoundData(id)?.PauseTalkSound();
    }



    // 改变音效声音大小
    public void ChangeSoundValue(float v, int id = 0)
    {
        GetSoundData(id)?.ChangeSoundValue(v);
    }

    // 设置音效静音
    public void SetSoundMute(bool mute, int id = 0)
    {
        GetSoundData(id)?.SetSoundMute(mute);
    }

    // 获得音效的音量
    public float GetSoundVolume(int id = 0)
    {
        return (float)GetSoundData(id)?.SoundVolume;
    }

    // 获得音效是否静音
    public bool SoundMuted(int id = 0)
    {
        return (bool)GetSoundData(id)?.SoundMuted;
    }

    #endregion

}


class SoundData
{
    private GameObject soundObj = null;
    private List<AudioSource> soundList = new List<AudioSource>();

    private string sound_volumeName;
    private string sound_mutedName;

    private int sound_muted;
    private float sound_volume;
    private string ObjName;
    private bool IsSingle;

    public float bgSoundVolume
    {
        get { return AppData.Instance.bgSound; }
    }

    public void OnInit(string _ObjName = "Sound", bool _IsSingle = true, string _sound_mutedName = null, string _sound_volumeName = null)
    {
        if (!string.IsNullOrEmpty(_sound_mutedName))
            this.sound_muted = PlayerPrefs.GetInt(_sound_mutedName, 0);

        if (!string.IsNullOrEmpty(_sound_volumeName))
            this.sound_volume = PlayerPrefs.GetFloat(_sound_volumeName, 1.0f);

        OnInit(this.sound_muted, this.sound_volume, _ObjName, _IsSingle, _sound_mutedName, _sound_volumeName);
    }

    public void OnInit(int _sound_muted, float _sound_volume, string _ObjName = "Sound", bool _IsSingle = true, string _sound_mutedName = null, string _sound_volumeName = null)
    {
        sound_muted = _sound_muted;
        sound_volume = _sound_volume;

        ObjName = _ObjName;
        IsSingle = _IsSingle;

        sound_mutedName = _sound_mutedName;
        sound_volumeName = _sound_volumeName;
    }

    public void Update()
    {
        if (!IsSingle)
        {
            for (int i = soundList.Count - 1; i >= 0; --i)
            {
                try
                {
                    if (!soundList[i].isPlaying)
                    {
                        GameObject.Destroy(soundList[i]);
                        soundList.RemoveAt(i);
                    }
                }
                catch { }
            }
        }
        else
        {

        }


        try
        {
            if (soundList != null && soundList.Count > 0)
            {
                if (soundList[0] != null && !soundList[0].isPlaying)
                {
                    EventMgr.Instance.EventTrigger("TS_Stop"); // 对话声音结束
                }
            }
        }
        catch { }
    }


    #region 播放音效
    // 播放音效和语音？
    public void PlaySound(string name, bool loop = false, UnityAction<AudioSource> callBack = null)
    {
        AudioSource source = null;
        if (soundObj == null)
            soundObj = new GameObject(ObjName);

        if (IsSingle && soundList.Count > 0)
        {
            source = soundList[0];
        }
        else
        {
            source = soundObj.AddComponent<AudioSource>();
            soundList.Add(source);
        }
        source.playOnAwake = false;
        source.clip = ResMgr.Instance.load_sound(name);
        source.loop = loop;
        source.mute = (this.sound_muted == 1);
        source.volume = (this.sound_volume * bgSoundVolume);
        source.Play();

        if (callBack != null)
            callBack(source);
    }

    // 停止音效
    public void StopSound(AudioSource source)
    {
        if (soundList.Contains(source))
        {
            source.Stop();
        }
    }

    // 停驶所有音效
    public void StopSound()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList[i].Stop();
        }
    }


    // 暂停对话语音
    public void PauseTalkSound(AudioSource source)
    {
        if (source == null)
            return;
        source.Pause();
    }

    // 停止对话语音
    public void PauseTalkSound()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList[i].Pause();
        }
    }



    // 改变音效声音大小
    public void ChangeSoundValue(float v)
    {
        this.sound_volume = v;
        if (soundList.Count <= 0)
            return;

        for (int i = 0; i < soundList.Count; i++)
        {
            soundList[i].volume = v * bgSoundVolume;
        }
        if (!string.IsNullOrEmpty(sound_volumeName))
            PlayerPrefs.SetFloat(sound_volumeName, this.sound_volume);
    }

    // 设置音效静音
    public void SetSoundMute(bool mute)
    {
        if (mute == (this.sound_muted == 1))
        {
            return;
        }

        this.sound_muted = mute ? 1 : 0;

        for (int i = 0; i < soundList.Count; i++)
        {
            soundList[i].mute = mute;
        }
        if (!string.IsNullOrEmpty(sound_mutedName))
            PlayerPrefs.SetInt(sound_mutedName, this.sound_muted);
    }



    // 获得音效的音量
    public float SoundVolume
    {
        get
        {
            return this.sound_volume;
        }
    }

    // 获得音效是否静音
    public bool SoundMuted
    {
        get
        {
            return (this.sound_muted == 1);
        }
    }

    #endregion
}

